﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Team4Mock.Entity;

namespace Team4Mock.UserControl.TrustRegion
{
    public partial class ViewAll : System.Web.UI.UserControl
    {
        TrustRegionEntity te = new TrustRegionEntity();
        OrganisationEntity oe=new OrganisationEntity();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Convert.ToString(Session["role"])))
                {
                    if (Session["role"].ToString().Equals("Admin"))
                    {
                        DropDownList1.DataSource = oe.GetCountry();
                        DropDownList1.DataBind();
                        dlNation.DataSource = oe.GetCountry();
                        dlNation.DataBind();
                        if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["ID"])))
                        {
                            try
                            {
                                pnView.Visible = false;
                                pnct.Visible = true;
                                Add.Visible = false;
                                int id = int.Parse(Request.QueryString["ID"]);
                                te.RegionID = id;
                                gvTrustDistrict.DataSource = te.GetDisTrict(te);
                                gvTrustDistrict.DataBind();
                                DataTable dt = new DataTable();
                                dt = te.GetdataByID(te);
                                if (dt.Rows.Count > 0)
                                {
                                    txtRegionName.Text = dt.Rows[0][1].ToString();
                                    txtDes.Text = dt.Rows[0][2].ToString();
                                    dlNation.SelectedValue = dt.Rows[0][3].ToString();
                                    if (dt.Rows[0][4].ToString() == "Yes")
                                    {
                                        ckisactive.Checked = true;
                                    }
                                    else
                                    {
                                        ckisactive.Checked = false;
                                    }
                                }
                            }
                            catch
                            {
                                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Error();", true);
                            }
                        }
                        else
                        {
                            pnView.Visible = true;
                            pnct.Visible = false;
                            Add.Visible = false;
                            LoadGV();
                        }
                    }
                    else
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Rediect();", true);
                    }
                }
                else
                {
                    Response.Redirect("/FormView/Login.aspx");
                }
            }
        }
        protected void btnCreate_Click(object sender, EventArgs e)
        {
            pnView.Visible = false;
            pnct.Visible = false;
            Add.Visible = true;
        }
        void LoadGV()
        {
            gvTrustRegion.DataSource = te.Getdata();
            gvTrustRegion.DataBind();
        }
        DataTable dt=new DataTable();
        protected void All_Click(object sender, EventArgs e)
        {
            dt = te.Getdata();
            if (dt.Rows.Count>0)
            {
                gvTrustRegion.DataSource = dt;
                gvTrustRegion.DataBind();
                lb.Text = "";
            }
            else
            {
                lb.Text = "No Record";
            }
            
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            te.Key = "ABCDE";
            dt = te.Search(te);
            if (dt.Rows.Count > 0)
            {
                gvTrustRegion.DataSource = dt;
                gvTrustRegion.DataBind();
                lb.Text = "";
            }
            else
            {
                lb.Text = "No Record";
            }
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            te.Key = "FGHIJ";
            dt = te.Search(te);
            if (dt.Rows.Count > 0)
            {
                gvTrustRegion.DataSource = dt;
                gvTrustRegion.DataBind(); lb.Text = "";
            }
            else
            {
                lb.Text = "No Record";
            }
        }

        protected void LinkButton4_Click(object sender, EventArgs e)
        {
            te.Key = "KMNL";
            dt = te.Search(te);
            if (dt.Rows.Count > 0)
            {
                gvTrustRegion.DataSource = dt;
                gvTrustRegion.DataBind(); lb.Text = "";
            }
            else
            {
                lb.Text = "No Record";
            }
        }

        protected void LinkButton5_Click(object sender, EventArgs e)
        {
            te.Key = "OPQR";
            dt = te.Search(te);
            if (dt.Rows.Count > 0)
            {
                gvTrustRegion.DataSource = dt;
                gvTrustRegion.DataBind(); lb.Text = "";
            }
            else
            {
                lb.Text = "No Record";
            }
        }

        protected void LinkButton6_Click(object sender, EventArgs e)
        {
            te.Key = "STUV";
            dt = te.Search(te);
            if (dt.Rows.Count > 0)
            {
                gvTrustRegion.DataSource = dt;
                gvTrustRegion.DataBind(); lb.Text = "";
            }
            else
            {
                lb.Text = "No Record";
            }
        }

        protected void LinkButton7_Click(object sender, EventArgs e)
        {
            te.Key = "WXYZ";
            dt = te.Search(te);
            if (dt.Rows.Count > 0)
            {
                gvTrustRegion.DataSource = dt;
                gvTrustRegion.DataBind(); lb.Text = "";
            }
            else
            {
                lb.Text = "No Record";
            }
        }


        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("/FormView/TrustRegion.aspx");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            //Amend
            if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["ID"])))
            {
                try
                {
                    int id = int.Parse(Request.QueryString["ID"]);
                    if (txtRegionName.Text == "")
                    {
                        tb.Text = "Input Region Name";
                    }
                    else
                    {
                        te.TrusRegionID = id;
                        te.TrusRegionName = txtRegionName.Text;
                        te.CountryID = dlNation.SelectedValue;
                        te.IsActive = ckisactive.Checked ? 1 : 0;
                        te.Description = txtDes.Text;
                        te.Update(te);
                        tb.Text = "Update Success";
                    }
                }
                catch
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Error();", true);
                }
            }
            else
            {
                if (txtRegionName.Text == "")
                {
                    tb.Text = "Input Region Name";
                }
                else
                {
                    try
                    {
                        te.TrusRegionName = txtRegionName.Text;
                        te.CountryID = dlNation.SelectedValue;
                        te.IsActive = ckisactive.Checked ? 1 : 0;
                        te.Description = txtDes.Text;
                        te.Insert(te);
                        tb.Text = "Insert Success";
                    }
                    catch
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Error();", true);
                    }
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("/FormView/TrustDistrict.aspx?t=" + Request.QueryString["ID"]);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "")
            {
                Label1.Text = "Input Region Name";
            }
            else
            {
                try
                {
                    te.TrusRegionName = TextBox1.Text;
                    te.CountryID = DropDownList1.SelectedValue;
                    te.IsActive = CheckBox3.Checked ? 1 : 0;
                    te.Description = TextBox2.Text;
                    te.Insert(te);
                    Label1.Text = "Insert Success";
                }
                catch
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Error();", true);
                }
            }
        }
    }
}